﻿#region MIT License

/*
 * Copyright (c) 2009 Nick Gravelyn (nick@gravelyn.com), Markus Ewald (cygon@nuclex.org)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 * 
 */

#endregion

using System.Collections.Generic;
using System.Xml;
using AnimationPackerEngine;
using AnimationPackerEngine.Exporters;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;
using SpriteAtlasRuntime;

namespace AnimatedSpritePacker.XnaExporters.MapExporters
{
    // Writes out two XML files ready to be put into an XNA Content project to be get compiled as content.
    // The first file is loaded using Content.Load<List<Rectangle>> from inside the game. It is a simple
    // list of rects for any un-animated sprites.
    // The second file is loaded using Content.Load<AnimationCollection>. It is a collection of all
    // scripts parsed and compiled into byte form.
    public class XmlMapExporter : IMapExporter
    {
        public string MapExtension
        {
            get { return "xml"; }
        }

        public void Save(string filename, ProcessingAnimationCollection animationCollection)
        {
            SpriteAtlasMap atlas = new SpriteAtlasMap();
            atlas.AnimationCollection.Scripts = new List<byte[]>();

            foreach (var anim in animationCollection.AnimationCollection)
            {
                foreach (var frame in anim.Frames)
                {
                    atlas.SpriteFrames.Add(frame.AtlasRect);
                }

                if (!anim.AnimationCompiler.SimpleList)
                {
                    List<byte[]> compiled = anim.AnimationCompiler.Compile(animationCollection);
                    atlas.AnimationCollection.Scripts.AddRange(compiled);
                }
            }

            XmlWriterSettings xmlSettings = new XmlWriterSettings();
            xmlSettings.Indent = true;
            xmlSettings.IndentChars = " ";

            using (XmlWriter writer = XmlWriter.Create(filename, xmlSettings))
            {
                IntermediateSerializer.Serialize(writer, atlas, null);
            }
        }
    }
}
